//
//  TableVC+NavBar.m
//  
//  jhf
//
//  Created by 计海峰 on 2020/8/19.
//

#import "TableVC+NavBar.h"

@implementation TableVC (NavBar)


- (void)configNavBar {
    if (self.navbarC) {
        // 隐藏之前导航栏
        self.fd_prefersNavigationBarHidden = true;
        // 修改tableview位置
        [self.tableview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).mas_offset(kNavigationBar_Height + kStatusBar_Height);
        }];
        // 标题
        self.navigationV.title = self.navigationItem.title;
        // 背景色
        if (self.navbarC.barNavBarColor) {
            self.navigationV.backgroundColor = self.navbarC.barNavBarColor;
        }else if (self.navbarC.barNavBarColorHex) {
            if ([TABLE_DEFAULT_CLEAR_COLOR_HEX isEqualToString:self.navbarC.barNavBarColorHex]) {
                self.navigationV.backgroundColor = [UIColor clearColor];
                [self.tableview mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.view).mas_offset(0);
                }];
            }else {
                self.navigationV.backgroundColor = [UIColor colorWithHexString:self.navbarC.barNavBarColorHex];
            }
        }
        
        // 返回按钮
        [self.navigationV.backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
        // 返回按钮图片
        if (self.navbarC.barBackImgName) {
            [self.navigationV.backButton setImage:[UIImage imageNamed:self.navbarC.barBackImgName] forState:0];
        }
        
        // 右侧按钮
        if (self.navbarC.rightBTTitle) {
            [self.navigationV.rightButton setTitle:self.navbarC.rightBTTitle forState:0];
        }
        if (self.navbarC.rightBTImgName) {
            [self.navigationV.rightButton setImage:[UIImage imageNamed:self.navbarC.rightBTImgName] forState:0];
        }
        if (self.navbarC.rightBTMethod) {
            //获取反射的方法
            SEL selector = NSSelectorFromString(self.navbarC.rightBTMethod);
            //判断是否有这个方法
            if ([self.vm respondsToSelector:selector]) {
                [self.navigationV.rightButton addTarget:self.vm action:selector forControlEvents:UIControlEventTouchUpInside];
            }
        }
        
        // 是否隐藏分割线
        self.navigationV.hidenShadow = self.navbarC.clearLine;
        // 中间字字体
        if (self.navbarC.barTitleColorHex && self.navbarC.barTitleFontSize) {
            self.navigationV.centerTitleLabel.textColor = [UIColor colorWithHexString:self.navbarC.barTitleColorHex];
            self.navigationV.centerTitleLabel.font = [UIFont systemFontOfSize:self.navbarC.barTitleFontSize];
        }
        if (self.navbarC.barTitleFont) {
            self.navigationV.centerTitleLabel.font = self.navbarC.barTitleFont;
        }
    }
}


//- (void)configNavBar; {
//    if (self.navbarC) {
//        if (self.navbarC.barNavBarColorHex) {
//            self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:self.navbarC.barNavBarColorHex];
//        }
//
//        if (self.navbarC.barTitleColorHex && self.navbarC.barTitleFontSize) {
//            NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
//                                                       [UIColor colorWithHexString:self.navbarC.barTitleColorHex],
//                                                       NSForegroundColorAttributeName,
//                                                       [UIFont systemFontOfSize:self.navbarC.barTitleFontSize],
//                                                       NSFontAttributeName,
//                                                       nil];
//            [self.navigationController.navigationBar setTitleTextAttributes:navbarTitleTextAttributes];
//        }
//        if (self.navbarC.barBackImgName) {
//            self.navigationItem.leftBarButtonItems = [JUtil barItemWithArray:[NSArray arrayWithObjects:self.navbarC.barBackImgName, nil] target:self action:@selector(back:)];
//        }
//        if (self.navbarC.clearLine) {
//            UIImageView *imgV = [self findUnderLineWithView:self.navigationController.navigationBar];
//            if (imgV) {
//                imgV.hidden = true;
//            }
//        }
//    }
//}

- (void)back:(id)any {
    [self.navigationController popViewControllerAnimated:YES];
}

//- (void)cancelNavBar; {
//    if (self.navbarC) {
//        if (self.navbarC.barNavBarColorHex) {
//            self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:jNavConfig.barNavBarColorHex];
//        }
//        if (self.navbarC.barTitleColorHex && self.navbarC.barTitleFontSize) {
//            NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
//                                                       [UIColor colorWithHexString:jNavConfig.barTitleColorHex],
//                                                       NSForegroundColorAttributeName,
//                                                       [UIFont systemFontOfSize:jNavConfig.barTitleFontSize],
//                                                       NSFontAttributeName,
//                                                       nil];
//            [self.navigationController.navigationBar setTitleTextAttributes:navbarTitleTextAttributes];
//        }
//    }
//}


// 隐藏底部黑线
- (UIImageView *)findUnderLineWithView:(UIView *)view {
    if (view.mj_h <= 1.0 && [view isKindOfClass:[UIImageView class]]) {
        return (UIImageView *)view;
    }
    for (UIView *v in view.subviews) {
        UIImageView *imageView = [self findUnderLineWithView:v];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

@end
